const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    modifyPasswordTabIndex:0,
    // 修改密码
    modifyPasswordVal:'',
    modifyNewPasswordVal:'',
    modifyConfirmPasswordVal:'',
    // 找会密码
    findPasswordPhoneVal:'',
    findPasswordCaptchaVal:'',
    findPasswordNewVal:'',
    findConfirmNewPasswordVal:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  retrievePassword(){
    wx.navigateTo({
      url:"/userPage/pages/retrievePassword/retrievePassword",
    })
  },

  changePassword(){
    wx.navigateTo({
      url:"/userPage/pages/payPasswordChange/payPasswordChange",
    })
  },



  // 切换
  modifyPasswordTab:function(e){
    var { index } = e.target.dataset
    this.setData({
      modifyPasswordTabIndex:index
    })
  },
  // 修改密码原密码
  modifyPasswordInp:function(e){
    this.setData({
      modifyPasswordVal:e.detail.value
    })
  },
  // 修改密码新原密码
  modifyNewPasswordInp: function (e) {
    this.setData({
      modifyNewPasswordVal: e.detail.value
    })
  },
  // 修改密码确认新原密码
  modifyConfirmPasswordInp: function (e) {
    this.setData({
      modifyConfirmPasswordVal: e.detail.value
    })
  },
  // 找会密码手机号
  findPasswordPhoneInp: function (e) {
    this.setData({
      findPasswordPhoneVal: e.detail.value
    })
  },
  // 找会密码验证码
  findPasswordCaptcha: function (e) {
    this.setData({
      findPasswordCaptchaVal: e.detail.value
    })
  },
  // 找回密码新密码
  findPasswordNewInp: function (e) {
    this.setData({
      findPasswordNewVal: e.detail.value
    })
  },
  // 找回密码确认新密码
  findConfirmNewPassword: function (e) {
    this.setData({
      findConfirmNewPasswordVal: e.detail.value
    })
  },
  // 获取验证码
  phoneCaptchaBtn:function(){
    var { findPasswordPhoneVal } = this.data
    if (findPasswordPhoneVal == ''){
      wx.showToast({
        title: '请输入手机号',
        icon: 'none',
        duration: 2000
      })
      return false
    }
    wx.showLoading({
      title: '加载中',
    })
    var url = app.globalData.baseUrl + 'user/sendCode';
    var data = {
      phone: findPasswordPhoneVal,
      type: 2
    }
    app.wxGetRequest(url, data, (res) => {
      console.log(res)
      wx.hideLoading()
      if (res.code == 1000) {
        wx.showToast({
          title: '发送成功',
          icon: 'success',
          duration: 2000
        })
      }else{
        wx.showToast({
          title: res.message,
          icon: 'none',
          duration: 2000
        })
      }
    }, (err) => {
      wx.hideLoading()
      wx.showToast({
        title: err.data.message,
        icon: 'none',
        duration: 2000
      })
    })
  },
  // 确定修改或找回
  modifBtn:function(){
    var { modifyPasswordTabIndex } = this.data
    var _this = this
    if (modifyPasswordTabIndex == 0){
      var { modifyPasswordVal, modifyNewPasswordVal, modifyConfirmPasswordVal } = this.data
      // 修改密码
      var url = app.globalData.baseUrl + 'user/updatePayCode';
      var data = {
        oldPass: modifyPasswordVal,
        password: modifyNewPasswordVal,
        repassword: modifyConfirmPasswordVal,
        token: app.globalData.token
      }
    }else{
      // 找回密码
      var { findPasswordPhoneVal, findPasswordCaptchaVal, findPasswordNewVal, findConfirmNewPasswordVal } = this.data
      var url = app.globalData.baseUrl + 'user/payCodeReset';
      var data = {
        phone: findPasswordPhoneVal,
        code: findPasswordCaptchaVal,
        password: findPasswordNewVal,
        repassword: findConfirmNewPasswordVal,
        token: app.globalData.token
      }
    }
    wx.showLoading({
      title: '加载中',
    })
    app.wxPostRequest(url, data, (res) => {
      console.log(res)
      wx.hideLoading()
      if (res.code == 1000) {
        wx.showToast({
          title: '修改成功',
          icon: 'success',
          duration: 2000
        })
        setTimeout(function () {
          wx.navigateBack({
            delta: 1
          })
        }, 2000)
      }else{
        wx.showToast({
          title: res.message,
          icon: 'none',
          duration: 2000
        })
      }
    }, (err) => {
      wx.hideLoading()

    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
